package com.cicc.springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cicc.springboot.common.dto.R;
import com.cicc.springboot.entity.MaterialZmaraEntity;
import com.cicc.springboot.mapper.MaterialInformationMapper;
import com.cicc.springboot.model.MaterialQuery;
import com.cicc.springboot.service.MaterialInformationService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLOutput;


/**
 * @ClassName MaterialInformatioServiceImpl
 * @Description TODO
 * @Author 转轮王
 * @Date 2024/9/30 16:04
 * @Version 1.0
 */


@Service
public class MaterialInformatioServiceImpl extends ServiceImpl<MaterialInformationMapper, MaterialZmaraEntity> implements MaterialInformationService {


    @Autowired
    private MaterialInformationMapper materialInformationMapper;

    //用于判断sql影响的行数
    private final int rows = 0;

    /**
     * 物料信息分页查询
     *
     * @param currentPage
     * @param pageSize
     * @param materialName
     * @param materialNumber
     * @param materialType
     * @param materialStatus
     * @return com.cicc.springboot.common.dto.R
     * @description: TODO
     * @author: 转轮王
     * @date: 2024/9/27 16:30
     */
    @Override
    public R<Page<MaterialZmaraEntity>> queryMaterialsPage(int currentPage, int pageSize, String materialName, String materialNumber, String materialType, String materialStatus) {

        Page<MaterialZmaraEntity> page = new Page<>(currentPage, pageSize);
        QueryWrapper<MaterialZmaraEntity> queryWrapper = new QueryWrapper<>();
        if (materialName != null && !materialName.isEmpty()) {
            queryWrapper.like("maktx_zh", materialName);
        }
        if (materialNumber != null && !materialNumber.isEmpty()) {
            queryWrapper.eq("matnr", materialNumber);
        }
        if (materialType != null && !materialType.isEmpty()) {
            queryWrapper.eq("mtart", materialType);
        }
        if (materialStatus != null && !materialStatus.isEmpty()) {
            queryWrapper.eq("material_state", materialStatus);
        }
        Page<MaterialZmaraEntity> materialZmaraEntityPage = materialInformationMapper.selectPage(page, queryWrapper);

        return R.ok(materialZmaraEntityPage);

    }


    /**
     * 根据物料id查询物料信息的详情
     *
     * @param id
     * @return
     */
// 根据id查看详情
    @Override
    public R<MaterialZmaraEntity> getMaterialById(Long id) {
        MaterialZmaraEntity materialZmaraEntity = materialInformationMapper.selectById(id);
        if (materialZmaraEntity != null) {
            // 查找到记录，返回成功响应
            return R.ok(materialZmaraEntity);
        } else {
            // 没有查找到记录，返回失败响应
            return R.failed("未找到ID为 " + id + " 的物料信息");
        }
    }

    /**
     * 新增或修改操作
     *
     * @param materialQuery
     * @return com.cicc.springboot.common.dto.R<com.cicc.springboot.entity.MaterialZmaraEntity>
     * @description: TODO
     * @author: 转轮王
     * @date: 2024/9/29 15:36
     */
    @Override
    public R<MaterialZmaraEntity> saveMaterial(MaterialQuery materialQuery) {
        if (materialQuery.getId() != null) {
            int updateCount = materialInformationMapper.updateById(materialQuery);
            if (updateCount > rows) {
                return R.ok(null, "修改成功");
            } else {
                return R.failed("更新失败 未找到该物料信息");
            }
        } else {
            int insertCount = materialInformationMapper.insert(materialQuery);
            if (insertCount > rows) {
                return R.ok(null, "新增成功");
            } else {
                return R.failed("新增失败");
            }
        }
    }

    /**
     * //删除物料信息
     *
     * @param MaterialId
     * @return
     * @description: TODO
     * @author: 转轮王
     * @date: 2024/9/29 15:35
     */
    @Override
    public R<Integer> deleteMaterial(Long MaterialId) {
        int deleteByCount = materialInformationMapper.deleteById(MaterialId);
        if (deleteByCount > rows) {
            return R.ok(deleteByCount, "删除成功");
        } else {
            return R.failed("删除失败 未找到该物料信息");
        }

    }


    /**
     * 根据物料id修改状态码
     *
     * @param materialId
     * @return com.cicc.springboot.common.dto.R<java.lang.Integer>
     * @description: TODO
     * @author: 转轮王
     * @date: 2024/9/29 15:34
     */
    @Override
    public R<String> updateMaterialState(Long materialId) {
        // 首先获取当前物料的状态
        MaterialZmaraEntity material = materialInformationMapper.selectById(materialId);
        if (material == null) {
            return R.failed("物料未找到");
        }

        // 获取当前状态并进行取反
        String currentState = material.getMaterialState();
        String newState;

        // 状态码为 "0"（启用）和 "1"（禁用）
        if ("0".equals(currentState)) {
            newState = "1"; // 当前状态是启用
        } else {
            newState = "0"; // 否则取反为启用
        }

        // 更新物料状态
        UpdateWrapper<MaterialZmaraEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", materialId)
                .set("material_state", newState);

        int updateCount = materialInformationMapper.update(null, updateWrapper);

        if (updateCount > 0) {
            return R.ok("修改成功");
        } else {
            return R.failed("修改失败");
        }
    }

}
